package com.fcy.myapplicati

import android.os.Bundle
import android.util.DisplayMetrics
import android.util.Log
import android.view.*
import androidx.fragment.app.DialogFragment

class MyDia : DialogFragment() {
    // 记录底部位置与顶部位置

    var top = 0
    var bot = 0
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        val inflate = inflater.inflate(R.layout.dia_view, container)
        inflate.setOnTouchListener(delegate)
        return inflate
    }


    override fun onStart() {
        super.onStart()
        println(targetFragment)
        dialog?.window?.setBackgroundDrawableResource(R.drawable.ic_launcher_background)
        val dm = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getRealMetrics(dm)
        dialog?.window?.attributes?.apply {
            gravity = Gravity.BOTTOM
            width = dm.widthPixels
            height = (dm.heightPixels * 0.5).toInt()
            dialog?.window?.attributes = this
            dimAmount = 0F
            flags or WindowManager.LayoutParams.FLAG_DIM_BEHIND
            dialog?.window?.attributes = this
            top = (dm.heightPixels * 0.5).toInt()
            bot = dm.heightPixels
        }


    }

    private val detector = object : GestureDetector.SimpleOnGestureListener() {

        override fun onScroll(
            e1: MotionEvent?,
            e2: MotionEvent?,
            distanceX: Float,
            distanceY: Float
        ): Boolean {
            val y1 = e1?.rawY
            val y2 = e2?.rawY
            Log.d(TAG, "onScroll: $distanceY $y2  $y1")
            changeLocation((distanceY / 10).toInt())
            if (e2?.action == MotionEvent.ACTION_UP) {
                //TODO 复原的逻辑 或者dismiss
                if (e2.rawY > (top + bot) / 2) {
                    dialog?.dismiss()
                } else {
                    dialog?.findViewById<View>(R.id.all)?.apply {
                        this.top = top
                        this.bottom = bottom
                    }
                }
            }
            return super.onScroll(e1, e2, distanceX, distanceY)
        }

    }
    private var lastY = 0F
    private var offset = 0F
    private var array = IntArray(2)
    private val delegate: (View, MotionEvent) -> Boolean = { view, motionEvent ->
        Log.d(TAG, ":${view.top} ${view.bottom} ${view.getLocationOnScreen(array)} $top $bot")
        motionEvent.apply {
            when (motionEvent.action) {
                MotionEvent.ACTION_UP -> {
                    view.getLocationOnScreen(array)
                    Log.d(TAG, ":up ${array[0]}  ${array[1]}")
                    if (array[1] < (bot + top) / 2) {
                        view.top = 0
                        view.bottom = view.height

                        //给一个动画效果


                    } else {
                        dialog?.dismiss()
                    }
                }
                MotionEvent.ACTION_DOWN -> lastY = rawY
                MotionEvent.ACTION_MOVE -> {
                    offset = rawY - lastY
                    lastY = rawY
                    Log.d(TAG, ": $offset")
                    if (offset < 0 && view.top == 0) {
                        return@apply
                    } else {
                        changeLocation(offset.toInt())
                    }
                }
            }
        }
        true
    }

    private fun changeLocation(distance: Int) {
        dialog?.findViewById<View>(R.id.all)?.apply {
            this.top += distance
            this.bottom += distance
        }
    }
}

private const val TAG = "MyDia"